# = test.admin.4.rb - Admin, Create and Delete User Test
# 
# See LICENSE.txt for licensing details.
#
# == Description
# This will test the functionality for creating and deleting users.
#
require 'watir'
require 'watir/WindowHelper'
include Watir
require 'test/unit'
require '../watir.config.rb'
require '../resources/functions.rb'

class TC_CreateAndDeleteUser_test < Test::Unit::TestCase
    
    def startup			
      $UserName = randomChar(10)
      $Password =  randomChar(12)
      $Email = randomChar(5) + '@' + randomChar(3) + '.com'
      @@ie = Watir::IE.start($SITE_URL)
    end
  
    def ie
      if defined? @@ie
        @@ie
      else
        startup
      end
    end

    def test_01_verifyHomeText
      assert(ie.contains_text(/Community Server/))			
    end
      
    def test_02_verifyLoginLinksExist
      if !(ie.link(:text, 'Sign in').exists?)
        ie.goto($SITE_URL + 'logout.aspx')
        ie.refresh
        ie.goto($SITE_URL)
        ie.refresh
      end
	  end
    
    def test_03_navigateToLogin
      ie.link(:text, 'Sign in').click
      assert(ie.contains_text("Sign in name"))
    end
	  
	  def test_04_populateLoginForm
      ie.text_field(:id, /username$/).value = $ADMIN_USER
      assert(ie.text_field(:id, /username$/).verify_contains($ADMIN_USER))
      
      ie.text_field(:id, /password$/).value = $ADMIN_PASSWORD
      assert(ie.text_field(:id, /password$/).verify_contains($ADMIN_PASSWORD))
	  end
	  
    def test_05_submitLoginForm
      ie.link(:id, /loginButton$/).click
      test_01_verifyHomeText
    end
    
    def test_06_verifyControlPanel
      assert(ie.link(:text, /Control Panel/).exists?)
      ie.link(:text, /Control Panel/).click
      assert(ie.contains_text(/Community Server Announcements/))      
    end
    
    def test_07_verifyControlPanelTabBarLinks
      assert_equal $SITE_URL + 'controlpanel/default.aspx', ie.link(:text, 'Dashboard').href
      assert_equal $SITE_URL + 'controlpanel/blogs', ie.link(:text, 'My Blogs').href
      assert_equal $SITE_URL + 'controlpanel/photos/', ie.link(:text, 'My Photos').href
      assert_equal $SITE_URL + 'controlpanel/files', ie.link(:text, 'My Files').href
      assert_equal $SITE_URL + 'controlpanel/moderation', ie.link(:text, 'Forums Moderation').href
      assert_equal $SITE_URL + 'controlpanel/Setup.aspx', ie.link(:text, 'Administration').href
      assert_equal $SITE_URL + 'controlpanel/Reporting/default.aspx', ie.link(:text, 'Reporting').href
    end
    
    def test_08_navigateToCreateUserPage
      ie.goto($SITE_URL + 'ControlPanel/Membership/useradd.aspx')
      assert(ie.contains_text("Create New Account"))
    end
    
    def test_09_verifyLinksExistOnCreateUserPage
      assert(ie.link(:text, 'Help').exists?)
      assert(ie.link(:text, 'Inbox').exists?)
      assert(ie.link(:text, 'Exit Control Panel and Return to Site').exists?)
      assert(ie.link(:text, 'Dashboard').exists?)
      assert(ie.link(:text, 'My Blogs').exists?)
      assert(ie.link(:text, 'My Photos').exists?)
      assert(ie.link(:text, 'My Files').exists?)
      assert(ie.link(:text, 'Forums Moderation').exists?)
      assert(ie.link(:text, 'Administration').exists?)
      assert(ie.link(:text, 'Reporting').exists?)
      assert(ie.link(:text, 'Find People').exists?)
      assert(ie.link(:text, 'Browse People').exists?)
      assert(ie.link(:text, 'Create New Account').exists?)
      assert(ie.link(:text, 'Manage All Roles').exists?)
      assert(ie.link(:text, 'New Registration Settings').exists?)
      assert(ie.link(:text, 'Global Account Profile Settings').exists?)
      assert(ie.link(:text, 'Member List Settings').exists?)
      assert(ie.link(:text, 'Cookie and Anonymous Settings').exists?)
      assert(ie.link(:text, 'Global Avatar Settings').exists?)
      assert(ie.link(:id, /Helpicon2$/).exists?)
      assert(ie.link(:id, /Helpicon1$/).exists?)
      assert(ie.link(:id, /Helpicon4$/).exists?)
      assert(ie.link(:id, /Helpicon3$/).exists?)
      assert(ie.link(:id, /createButton$/).exists?)
      assert(ie.link(:text, 'Telligent').exists?)
    end
    
    def test_10_verifyFieldsExistOnCreateUserPage
      assert(ie.select_list(:id, /timezone$/).exists?)
      assert(ie.text_field(:id, /username$/).exists?)
      assert(ie.text_field(:id, /password$/).exists?)
      assert(ie.text_field(:id, /password2$/).exists?)
      assert(ie.text_field(:id, /email$/).exists?)
    end
    
    def test_11_createNewUser
      ie.text_field(:id, /username$/).value = $UserName
      ie.text_field(:id, /password$/).value = $Password
      ie.text_field(:id, /password2$/).value = $Password
      ie.text_field(:id, /email$/).value = $Email
      
      ie.link(:id, /createButton$/).click
      
      assert(ie.contains_text('The user'))
      assert(ie.contains_text($UserName))
      assert(ie.contains_text('was created successfully'))
    end
    
    def test_12_verifyLinksExistOnSearchMembersPage
      	assert(ie.link(:text, 'Help').exists?)
        assert(ie.link(:text, 'Inbox').exists?)
        assert(ie.link(:text, 'Exit Control Panel and Return to Site').exists?)
        assert(ie.link(:text, 'Dashboard').exists?)
        assert(ie.link(:text, 'My Blogs').exists?)
        assert(ie.link(:text, 'My Photos').exists?)
        assert(ie.link(:text, 'My Files').exists?)
        assert(ie.link(:text, 'Forums Moderation').exists?)
        assert(ie.link(:text, 'Administration').exists?)
        assert(ie.link(:text, 'Reporting').exists?)
        assert(ie.link(:text, 'Find People').exists?)
        assert(ie.link(:text, 'Browse People').exists?)
        assert(ie.link(:text, 'Create New Account').exists?)
        assert(ie.link(:text, 'Manage All Roles').exists?)
        assert(ie.link(:text, 'New Registration Settings').exists?)
        assert(ie.link(:text, 'Global Account Profile Settings').exists?)
        assert(ie.link(:text, 'Member List Settings').exists?)
        assert(ie.link(:text, 'Cookie and Anonymous Settings').exists?)
        assert(ie.link(:text, 'Global Avatar Settings').exists?)
        assert(ie.link(:id, /_searchButton$/).exists?)
        assert(ie.link(:id, /EditUserLink$/).exists?)
        assert(ie.link(:id, /AddUserLink$/).exists?)
        assert(ie.link(:id, /linkTopPosters$/).exists?)
        assert(ie.link(:id, /linkVisitors$/).exists?)
        assert(ie.link(:id, /linkRegistrations$/).exists?)
        assert(ie.link(:id, /linkApproval$/).exists?)
        assert(ie.link(:text, 'Telligent').exists?)
    end

    def test_13_verifyFieldsExistOnSearchMembersPage
        assert(ie.select_list(:id, /memberSortDropDown$/).exists?)
        assert(ie.select_list(:id, /sortOrderDropDown$/).exists?)
        assert(ie.select_list(:id, /joinedDateComparer$/).exists?)
        assert(ie.select_list(:id, /lastPostDateComparer$/).exists?)
        assert(ie.select_list(:id, /searchRole$/).exists?)
        assert(ie.select_list(:id, /searchType$/).exists?)
        assert(ie.select_list(:id, /currentAccountStatus$/).exists?)
        assert(ie.text_field(:id, /searchText$/).exists?)
    end
    
    def test_14_logoutAndLoginAsNewUser
      if !(ie.link(:id, /Login$/).exists?)
        ie.goto($SITE_URL + 'logout.aspx')
        ie.refresh
        ie.goto($SITE_URL)
        ie.refresh
      end
      
      assert(ie.link(:text, 'Sign in').exists?)
      assert(ie.link(:text, 'Join').exists?)
      assert(ie.link(:text, 'Help').exists?)
      
      ie.link(:text, 'Sign in').click
      
      ie.text_field(:id, /username$/).value = $UserName
      assert(ie.text_field(:id, /username$/).verify_contains($UserName))
      
      ie.text_field(:id, /password$/).value = $Password
      assert(ie.text_field(:id, /password$/).verify_contains($Password))
      
       ie.link(:id, /loginButton$/).click
      test_01_verifyHomeText
    end
    
    def test_15_logoutAndLoginAsAdmin
      if !(ie.link(:text, 'Sign in').exists?)
        ie.goto($SITE_URL + 'logout.aspx')
        ie.refresh
        ie.goto($SITE_URL)
        ie.refresh
      end
      
      assert(ie.link(:text, 'Sign in').exists?)
      assert(ie.link(:text, 'Join').exists?)
      assert(ie.link(:text, 'Help').exists?)
      
      ie.link(:text, 'Sign in').click
      
      ie.text_field(:id, /username$/).value = $ADMIN_USER
      assert(ie.text_field(:id, /username$/).verify_contains($ADMIN_USER))
      
      ie.text_field(:id, /password$/).value = $ADMIN_PASSWORD
      assert(ie.text_field(:id, /password$/).verify_contains($ADMIN_PASSWORD))
      
       ie.link(:id, /loginButton$/).click
    end
     
    def test_16_navigateToFindPersonPage
      ie.goto($SITE_URL + 'ControlPanel/Membership/default.aspx')
      assert(ie.contains_text('Find People'))
    end
    
    def test_17_verifyLinksExistOnFindPersonPage
      assert(ie.link(:text, 'Help').exists?)
      assert(ie.link(:text, 'Inbox').exists?)
      assert(ie.link(:text, 'Exit Control Panel and Return to Site').exists?)
      assert(ie.link(:text, 'Dashboard').exists?)
      assert(ie.link(:text, 'My Blogs').exists?)
      assert(ie.link(:text, 'My Photos').exists?)
      assert(ie.link(:text, 'My Files').exists?)
      assert(ie.link(:text, 'Forums Moderation').exists?)
      assert(ie.link(:text, 'Administration').exists?)
      assert(ie.link(:text, 'Reporting').exists?)
      assert(ie.link(:text, 'Find People').exists?)
      assert(ie.link(:text, 'Browse People').exists?)
      assert(ie.link(:text, 'Create New Account').exists?)
      assert(ie.link(:text, 'Manage All Roles').exists?)
      assert(ie.link(:text, 'New Registration Settings').exists?)
      assert(ie.link(:text, 'Global Account Profile Settings').exists?)
      assert(ie.link(:text, 'Member List Settings').exists?)
      assert(ie.link(:text, 'Cookie and Anonymous Settings').exists?)
      assert(ie.link(:text, 'Global Avatar Settings').exists?)
      assert(ie.link(:id, /searchButton$/).exists?)
      assert(ie.link(:id, /linkTopPosters$/).exists?)
      assert(ie.link(:id, /linkVisitors$/).exists?)
      assert(ie.link(:id, /linkRegistrations$/).exists?)
      assert(ie.link(:id, /linkApproval$/).exists?)
      assert(ie.link(:text, 'Telligent').exists?)
    end
    
    def test_18_verifyFieldsOnFindPersonPage
      assert(ie.select_list(:id, /memberSortDropDown$/).exists?)
      assert(ie.select_list(:id, /sortOrderDropDown$/).exists?)
      assert(ie.select_list(:id, /joinedDateComparer$/).exists?)
      assert(ie.select_list(:id, /lastPostDateComparer$/).exists?)
      assert(ie.select_list(:id, /searchRole$/).exists?)
      assert(ie.select_list(:id, /searchType$/).exists?)
      assert(ie.select_list(:id, /currentAccountStatus$/).exists?)
      assert(ie.text_field(:id, /searchText$/).exists?)
    end
    
    def test_19_findNewlyCreatedUser
      ie.text_field(:id, /searchText$/).value = $UserName
      ie.link(:id, /searchButton$/).click
      
      assert(ie.link(:text, $UserName).exists?)
    end
    
    def test_20_performDeleteUserAction
      assert(ie.link(:text, 'Actions').exists?)
      ie.link(:text, 'Actions').click
      
      #Item on menu to delete user
      assert(ie.cell(:id, /Menu_3$/).exists?)
      ie.cell(:id, /Menu_3$/).click
      
      assert(ie.frame(:index, 4).radio(:id, /AssignToAnonymous$/).exists?)
      ie.frame(:index, 4).radio(:id, /AssignToAnonymous$/).set
      
      assert(ie.frame(:index, 4).button(:id, /DeleteUserButton$/).exists?)
      ie.frame(:index, 4).button(:id, /DeleteUserButton$/).click
    end
    
    def test_21_verifyUserDeleted
      sleep 5
      ie.refresh
      sleep 5
      ie.refresh
      
      ie.text_field(:id, /searchText$/).value = $UserName
      ie.link(:id, /searchButton$/).click
    end

end